package com.syt.hospital.controller.api;


import com.syt.exception.SYTException;
import com.syt.helper.HttpRequestHelper;
import com.syt.hospital.service.DepartmentService;
import com.syt.hospital.service.HospitalService;
import com.syt.hospital.service.HospitalSetService;
import com.syt.hospital.service.ScheduleService;
import com.syt.model.hospital.Department;
import com.syt.model.hospital.Hospital;
import com.syt.model.hospital.Schedule;
import com.syt.result.Result;
import com.syt.result.ResultCodeEnum;
import com.syt.utils.MD5;
import com.syt.vo.hospital.DepartmentQueryVo;
import com.syt.vo.hospital.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author: xxx
 * @Date: 2023/2/9 21:47
 * @Description:
 */
@Api(tags = "接口管理")
//@CrossOrigin 有 gateway 做了跨域
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    @ApiOperation(value = "删除排班")
    @PostMapping("/schedule/remove")
    public Result remove(HttpServletRequest request){
        Map<String, Object> paramMap = requestParam(request);

        //医院编号
        String hoscode = (String) paramMap.get("hoscode");

        //排班编号
        String hosScheduleId = (String) paramMap.get("hosScheduleId");

        scheduleService.remove(hoscode,hosScheduleId);
        return Result.ok();


    }

    @ApiOperation(value="查询排班所有数据接口")
    @PostMapping("/schedule/list")
    public Result findSchedule(HttpServletRequest request){
        Map<String, Object> paramMap = requestParam(request);

        //医院编号
        String hoscode = (String) paramMap.get("hoscode");
        //科室编号
        String depcode = (String) paramMap.get("depcode");
        //当前页和每页记录数
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String) paramMap.get("limit"));

        //查询排班数据并分页
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);

        Page<Schedule> pageModel = scheduleService.findPageDepartment(page, limit, scheduleQueryVo);

        return Result.ok(pageModel);
    }


    @ApiOperation(value = "上传排班接口")
    @PostMapping("/saveSchedule")
    public Result savaSchedule(HttpServletRequest request) {
        Map<String, Object> paramMap = requestParam(request);
        scheduleService.save(paramMap);
        return Result.ok();
    }


    /*------------------------------------科室-------------------------------------------------------*/
    @ApiOperation(value = "删除科室接口")
    @PostMapping("/department/remove")
    public Result removeDepartment(HttpServletRequest request) {

        Map<String, Object> paramMap = requestParam(request);
        //医院编号
        String hoscode = (String) paramMap.get("hoscode");
        //科室编号
        String depcode = (String) paramMap.get("depcode");
        departmentService.remove(hoscode, depcode);
        return Result.ok();
    }


    @ApiOperation(value = "查询科室接口")
    @PostMapping("/department/list")
    public Result findDepartment(HttpServletRequest request) {

        /*//获取传递过来医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);


        //1、根据传递过来医院编码，查询数据库，查询签名
        String hoscode = (String) paramMap.get("hoscode");

        //2、获取医院系统传递过来的签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");

        String sinKey = hospitalSetService.getSinKey(hoscode);

        //3、把数据库查询签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(sinKey);

        //4、判断签名是否一致
        if (!hospSign.equals(signKeyMd5)) {
            throw new SYTException(ResultCodeEnum.SIGN_ERROR);
        }*/

        Map<String, Object> paramMap = requestParam(request);
        //医生编号
        String hoscode = (String) paramMap.get("hoscode");
        //当前页和每页记录数
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String) paramMap.get("limit"));

        //查询数据并分页
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);

        Page<Department> pageModel = departmentService.findPageDepartment(page, limit, departmentQueryVo);

        return Result.ok(pageModel);
    }


    @ApiOperation(value = "saveDepartment")
    @PostMapping("/saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
       /* //获取传递过来医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);


        //1、根据传递过来医院编码，查询数据库，查询签名
        String hoscode = (String) paramMap.get("hoscode");

        //2、获取医院系统传递过来的签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");

        String sinKey = hospitalSetService.getSinKey(hoscode);

        //3、把数据库查询签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(sinKey);

        //4、判断签名是否一致
        if (!hospSign.equals(signKeyMd5)) {
            throw new SYTException(ResultCodeEnum.SIGN_ERROR);
        }*/

        Map<String, Object> paramMap = requestParam(request);

        //调用service，保存科室表中
        departmentService.saveDepartment(paramMap);

        return Result.ok();

    }


    /*------------------------------------------------医院-------------------------------------------------------------------------------------*/


    @ApiOperation(value = "查询医院测试")
    @PostMapping("/hospital/show1")
    public Result getHospital() {
         return Result.ok( hospitalService.findList());
    }


    @ApiOperation(value = "查询医院")
    @PostMapping("/hospital/show")
    public Result getHospital(HttpServletRequest request) {
        /*//获取传递过来医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);


        //1、根据传递过来医院编码，查询数据库，查询签名
        String hoscode = (String) paramMap.get("hoscode");

        //2、获取医院系统传递过来的签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");

        String sinKey = hospitalSetService.getSinKey(hoscode);

        //3、把数据库查询签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(sinKey);

        //4、判断签名是否一致
        if (!hospSign.equals(signKeyMd5)) {
            throw new SYTException(ResultCodeEnum.SIGN_ERROR);
        }*/

        Map<String, Object> paramMap = requestParam(request);

        //调用service方法实现根据医院编号查询
        Hospital hospital = hospitalService.getHospitalByHoscode((String) paramMap.get("hoscode"));
        return Result.ok(hospital);
    }


    @ApiOperation("上传医院接口")
    @PostMapping("/saveHospital")
    public Result saveHospital(HttpServletRequest request) {
       /* //获取传递过来医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //1、获取医院系统传递过来的签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");
        //2、根据传递过来医院编码，查询数据库，查询签名
        String hoscode = (String) paramMap.get("hoscode");

        String sinKey = hospitalSetService.getSinKey(hoscode);

        //3、把数据库查询签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(sinKey);

        //4、判断签名是否一致
        if (!hospSign.equals(signKeyMd5)) {
            throw new SYTException(ResultCodeEnum.SIGN_ERROR);
        }*/

        Map<String, Object> paramMap = requestParam(request);

        //传输过程中"+"转换为" ",因此我们要转换回来
        String logoData = (String) paramMap.get("logoData");
        logoData = logoData.replaceAll(" ", "+");
        paramMap.put("logoData", logoData);

        //调用service的方法
        hospitalService.saveHospital(paramMap);
        return Result.ok();
    }


    /*---------------------------公共方法--------------------*/
    private Map<String, Object> requestParam(HttpServletRequest request) {
        //获取传递过来医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //1、根据传递过来医院编码，查询数据库，查询签名
        String hoscode = (String) paramMap.get("hoscode");

        //2、获取医院系统传递过来的签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");

        String sinKey = hospitalSetService.getSinKey(hoscode);

        //3、把数据库查询签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(sinKey);


        //4、判断签名是否一致
        if (!hospSign.equals(signKeyMd5)) {
            throw new SYTException(ResultCodeEnum.SIGN_ERROR);
        }
        return paramMap;
    }


}
    